import { axiosMock, delayFn, randArrItem } from '@/services/axiosMock'
import { IRoute } from '../AddRouteModal/interface'
import { IPublishRouteCheckResultItem, IPublishRouteListItem } from '../interface'

let count = 0
const img1 = `https://fba-srm.oss-cn-hongkong.aliyuncs.com/1734260133964_test1.png`
const img2 = `https://fba-srm.oss-cn-hongkong.aliyuncs.com/1734260167384_test2.png`

axiosMock('default', mock => {
  // 获取发布路由列表项
  mock.onPost(`/tmsapi/route_publish/list`).reply(
    delayFn<IPublishRouteListItem[]>(600, () => {
      const idPrefix = ++count
      return {
        success: true,
        data: Array(randArrItem([10, 20, 30]))
          .fill(null)
          .map((_item, index) => {
            const id = +`${idPrefix}${index + 1}`
            return {
              publishId: id,
              routeId: id,
              waybillNo: `LH013212081${id}`,
              routeNo: randArrItem([`${100 + id}`], 0.1),
              routeNode: randArrItem([0, 1, 2, 3, 4, 5, 6, 7, 8], 0.1),
              routeTitle: `已通过拖车运输，待抵达港口${id}`,
              routeType: randArrItem([1, 2], 0.1),
              occurDate: randArrItem(['2023-11-16 12:12:12', '2024-11-16 12:12:12'], 0.1),
              keepDate: randArrItem(['24', '36'], 0.1),
              fileList: randArrItem(
                [
                  [{ fileName: '附件名附件名附件名1', fileUrl: img1 }],
                  [{ fileName: '附件名附件名附件名附件名附件名附件名附件名附件名附件名2', fileUrl: img2 }],
                  [
                    { fileName: '附件名附件名附件名1', fileUrl: img1 },
                    { fileName: '附件名附件名附件名附件名附件名附件名附件名附件名附件名2', fileUrl: img2 },
                  ],
                ],
                0.1,
              ),
              publishStatus: randArrItem([0, 1], 0.1),
              markFlag: randArrItem([0, 0, 0, 1], 0.1),
            } satisfies IPublishRouteListItem
          }),
      }
    }),
  )

  // 删除发布路由
  mock.onPost(`/tmsapi/route_publish/del`).reply(
    delayFn(600, () => {
      return {
        success: true,
      }
    }),
  )

  // 发布校验
  mock.onPost(`/tmsapi/route_publish/validateRoute`).reply(
    delayFn(600, () => {
      const isValid = randArrItem([false, true, true])
      return {
        success: true,
        data: isValid
          ? null
          : Array(randArrItem([2, 3, 4, 7]))
              .fill(null)
              .map<IPublishRouteCheckResultItem>((_, i) => ({
                attrKey: `字段${i + 1}`,
                attrValue: '',
                type: randArrItem(['dynamic_field', 'address', 'date']),
              })),
      }
    }),
  )

  // 发布
  mock.onPost(`/tmsapi/route_publish/publish`).reply(
    delayFn(600, () => {
      return {
        success: true,
      }
    }),
  )

  // 获取运单下的路由列表
  mock.onPost(`/tmsapi/route_publish/listRouteTemplate`).reply(
    delayFn(600, () => {
      return {
        success: true,
        data: Array(randArrItem([10, 20, 30]))
          .fill(null)
          .map((_item, index) => {
            return {
              routeId: index,
              routeNo: randArrItem([`${100 + index}`], 0.1),
              templateCn: randArrItem(
                [
                  `中文模板标题\n中文模板中文模板中文模板中文模板中文模板\n 中文模板中文模板中文模板中文模板中文模板中文模板 \n 中文模板中文模板中文模板  中文模板中文模板中文模板-${index}`,
                ],
                0.1,
              ),
              templateEn: randArrItem(
                [
                  `英文模板标题\n英文模板英文模板英文模板英文模板英文模板\n 英文模板英文模板英文模板英文模板英文模板英文模板 \n 英文模板英文模板英文模板  英文模板英文模板英文模板-${index}`,
                ],
                0.1,
              ),
              routeNode: randArrItem([0, 1, 2, 3, 4, 5, 6, 7, 8], 0.1),
              logisticsStatus: randArrItem([0, 1, 2, 3, 4, 5, 6, 7], 0.1),
              routeType: randArrItem([1, 2], 0.1),
              actionFlag: randArrItem([0, 1]),
              visibleFlag: randArrItem([0, 1]),
            } satisfies IRoute
          }),
      }
    }),
  )

  // 新增路由
  mock.onPost(`/tmsapi/route_publish/add`).reply(
    delayFn(600, () => {
      return {
        success: true,
      }
    }),
  )
})
